package com.globallogic.vkulyk.SSHTool;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.jcraft.jsch.Session;

public class Unit_ScriptExecutor extends SSHToolTester {
	private static final String OUTPUT_NOT_EQUALS = "Output not equals";
	private final ByteArrayOutputStream outActual = new ByteArrayOutputStream();

	@Before
	public void setup() {
		setVarsFromPropertiesFile("testData/testing.properties");
		System.setOut(new PrintStream(outActual));
	}

	@Test
	public void shouldExecuteScript_whenCallExecute() {
		// given
		String outExpected = "Linux";

		// when
		new ScriptExecutor(host, port, user, password, script).execute();
		String outActualWithRemovedNewLine = outActual.toString().replaceAll(
				"\\n", "");

		// then
		assertEquals(OUTPUT_NOT_EQUALS, outExpected,
				outActualWithRemovedNewLine);
	}

	@Test
	public void shouldExecuteScript_whenCallExecuteInExistingSession() {
		// given
		ActionExecutor actionExecutor = new ActionExecutor(host, port, user,
				password);
		Session session = actionExecutor.openSession();
		String outExpected = "Linux";

		// when
		new ScriptExecutor(host, port, user, password, script)
				.executeInExistingSession(session);
		String outActualWithRemovedNewLine = outActual.toString().replaceAll(
				"\\n", "");

		// then
		assertEquals(OUTPUT_NOT_EQUALS, outExpected,
				outActualWithRemovedNewLine);

		actionExecutor.closeSession(session);
	}

	@After
	public void cleanup() {
		System.setOut(null);
	}

}
